﻿$(function () {

    $('#searchButton').click(function (e) {
        searchClose(e);
        CategorySetting();
    });


    CategorySetting();

    function CategorySetting() {

        $("#list").HimallDatagrid({
            url: './GetCategorySettingList',
            nowrap: false,
            rownumbers: true,
            NoDataMsg: '没有找到符合条件的限时购活动',
            border: false,
            fit: true,
            fitColumns: true,
            pagination: true,
            idField: "Id",
            pageSize: 15,
            pagePosition: 'bottom',
            pageNumber: 1,
            queryParams: { smallProgram: $("#selectSmallProgram").val(), },
            columns:
            [[
                { field: "Id", title: 'Id', hidden: true, width: 150 },
                { field: "PName", title: '小程序名称', width: 100 },

                { field: "WXSPCategory", title: "跳转类型", width: 150, align: "center" },
                 { field: "Title", title: "标签名称", width: 150, align: "center" },
               {
                   field: "Image", title: '图标', align: 'center',
                   formatter: function (value, row, index) {
                       var html = "";
                       html += '<img  id="img' + row.Id + '" style="width="118" height="118" src="' + row.Image + '?' + Date() + '" />';
                       return html;
                   }
               }, {
                   field: "DisplaySequence", title: '排序', align: 'center',
                   formatter: function (value, row, index) {
                       var html = "";
                       html += '<input class="text-order" type="text" value="' + row.DisplaySequence + '" />';
                       return html;
                   }
               },

               { field: "Content", title: "内容", width: 150, align: "center" },

            {
                field: "operation", operation: true, width: 150, title: "操作",
                formatter: function (value, row, index) {
                    var id = row.Id.toString();
                    var html = ["<span class=\"btn-a\">"];
                    html.push("<a class=\"good-check\" href=\"./EditCategorySetting/" + id + "\">编辑</a>");

                    //html.push("<a class=\"good-check\" href=\"./detail/" + id + "\">查看</a>");
                    html.push("<a class=\"good-check\" onclick=\"DeleteCategorySetting('" + id + "')\">删除</a>");
                    html.push("</span>");
                    return html.join("");
                }
            }
            ]]
        });

    }



    $("#AddItem").click(function () {
        location.href = "./AddCategorySetting";
    });
    var _order = 0;
    $('.container').on('focus', '.text-order', function () {
        _order = parseInt($(this).val());
    });

    $('.container').on('blur', '.text-order', function () {
        var id = $(this).parents('tr').find('td[field="Id"]').text();
        var depth = $(this).parents('tr').find('.hidden_depth').val();

        if (isNaN($(this).val()) || parseInt($(this).val()) <= 0) {
            $.dialog({
                title: '更新分类信息',
                lock: true,
                width: '400px',
                padding: '20px 60px',
                content: ['<div class="dialog-form">您输入的序号不合法,此项只能是大于零的整数.</div>'].join(''),
                button: [
                {
                    name: '关闭',
                }]
            });
            $(this).val(_order);
        } else {
            if (parseInt($(this).val()) === _order) return;
            var loading = showLoading();
            ajaxRequest({
                type: 'GET',
                url: "./UpdateCategoryOrder",
                param: { id: id, order: parseInt($(this).val()) },
                dataType: "json",
                success: function (data) {
                    loading.close();
                    if (data.success == true) {
                        $.dialog.tips('更新排序成功.');
                    }
                    else {
                        $.dialog.errorTips(data.msg, function () { location.reload(); });
                    }
                }, error: function () {
                    loading.close();
                }
            });

        }
    });
});

function DeleteBroadcastSetting(id) {
    $.dialog.confirm('是否确认删除？', function () {
        var loading = showLoading();
        ajaxRequest({
            type: 'POST',
            url: "./DeleteCategorySetting",
            param: { id: id },
            dataType: "json",
            success: function (data) {
                if (data.success == true) {
                    $.dialog.succeedTips("删除成功！", function () {
                        var pageNo = $("#list").HimallDatagrid('options').pageNumber;
                        $("#list").HimallDatagrid('reload', { pageNumber: pageNo });
                    }, 1);
                    loading.close();
                } else {
                    loading.close();
                    $.dialog.errorTips(data.msg);
                }
            }, error: function () {
                loading.close();
            }
        });
    });
}